<h1>Cookie Jacker</h1>

<script type="text/javascript">

function setNewCookie(){
	var key = document.getElementById('txtCookieName').value;
	var val = document.getElementById('txtCookieValue').value;
	
	if(key.length == 0) return;
	
	setCookie(key, val);
	
	writeAllCookies();
}


function setLotsOfCookies(){
	var i;
	for(i=0; i < 200; i++){
		setCookie("testCookie_" + i, i);		
	}
	writeAllCookies();
}


function setCookie(key, value){
	var cook = key + '=' + value + '; expires=1 Jan 2050; path=/';
	document.cookie=cook;
}

function readCookie(key){
	var cookies = document.cookie;
	var keyName = key + '=';
	var pos = cookies.indexOf(keyName);
	if(pos > -1){
		pos += keyName.length;
		var endPos = cookies.indexOf(';', pos);
		if(-1 == endPos){
			endPos = cookies.length;
		}
		return cookies.substring(pos, endPos);
	}
	return null;
}

/**
 * First round design to load app data and viewer
 */
function getInitialData(){
	writeAllCookies();
}


function deleteAllCookies(){
	var cookies = document.cookie.split(';');
	
	var expires = ";expires=1 Jan 1950;path=/";
	var kparts;
	for(var i=0; i < cookies.length; i++){
		kparts = cookies[i].split('=');
		document.cookie = kparts[0] + '=' + expires;
	}
	writeAllCookies();
}


function writeAllCookies(){
	var tbl = document.getElementById('cookieTable');
	
	var body = tbl.getElementsByTagName("tbody")[0];
	
	while(body.childNodes.length > 0){
		body.removeChild(body.firstChild);
	}
	
	var cookies = document.cookie.split(';');
	if(cookies.length == 1 && cookies[0] == "") return;
	
	var kparts;
	for(var i=0; i < cookies.length; i++){
		kparts = cookies[i].split('=');
		addTableRow(body, kparts[0], kparts[1]);
	}	
	var ht = Math.min((25 * (1 + body.childNodes.length)), 400);
	body.style.height=ht+"px";
}


function addTableRow(table, cell1, cell2){
	var row = document.createElement("tr");
	row.appendChild(document.createElement("td"));
	row.appendChild(document.createElement("td"));
	
	row.childNodes[0].innerHTML = cell1;
	row.childNodes[1].innerHTML = cell2;
	table.appendChild(row);
}


function log(message){
	var errFeed = document.getElementById("messages");
	if(errFeed){
		errFeed.innerHTML += message + "<br />";
	}
}


//register to look for initial data
gadgets.util.registerOnLoadHandler(getInitialData);



</script>

<style>
h1{
	font-size:1.2em;
}
#messages{
	height:20px;
	overflow:auto;
}

#cookieTable{
	width:400px;
	border:2px;
	padding:3px;
	font-family: Arial;
	font-size:10pt;
}
#cookieTable th{
	font-weight:bold;
	background-color: #DDD;
}
#cookieTable td{
	border: 1px solid #555;
	padding: 5px;
}

#cookieTable tbody{
	height: 400px;
	overflow: auto;

}

</style>

<div id="infoblock">
<form onsubmit="return false;">

Cookie Name:&nbsp;&nbsp;<input id="txtCookieName" name="name" /><br />
Cookie Value:&nbsp;&nbsp;<input id="txtCookieValue" name="val" /><br />
<button onclick="setNewCookie()">Set Cookie</button>
<br />
<button onclick="setLotsOfCookies()">Set Lots of Cookies</button>
<button onclick="deleteAllCookies()">Delete All Cookies</button>

<table id="cookieTable">
	<thead>
<tr><th>Cookie Name</th><th>Cookie Value</th></tr>
</thead>
<tbody>
	
</tbody>
</table>

<div style="font-family:courier,monotype;color:#555;height:150px;overflow:auto;" id="messages" >
</div>

</form>
</div>

